package at.dl.android.dota.adviser;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import at.dl.android.dota.adviser.R;

public class ListHeroActivity extends ListActivityWithDB {

    public final static String HERO_NAME1 = "HERO_NAME1"; 
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate ( Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_heroes);
        
        final Bundle extras = getIntent().getExtras();
        final String where = extras.getString(SearchHeroActivity.SEARCH_HERO_WHERE_CAUSE);
        final String query = "SELECT name1 AS _id, name2, hero_image_id FROM HEROES" + where;
        SQLiteDatabase db = getDataBaseHelper().getReadableDatabase();
        final Cursor cursor = db.rawQuery(query,new String[0]);
        startManagingCursor(cursor); // do we need that?
        setListAdapter(new MyEntryAdapter(this,cursor));
    }

    @Override
    protected void onListItemClick ( ListView l, View v, int position, long id ) {
        super.onListItemClick(l,v,position,id);
        
        TextView t = (TextView) v.findViewById(R.id.name1);
        String name1 = t.getText().toString();
        onPause();
        Intent myIntent = new Intent(v.getContext(), ShowHeroActivity.class);
        myIntent.putExtra(HERO_NAME1,name1);
        startActivity(myIntent);
    }
    
    private class MyEntryAdapter extends CursorAdapter {

        private final LayoutInflater mInflater;

        public MyEntryAdapter ( Context context, Cursor cursor ) {
            super(context,cursor,false);
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public void bindView ( View view, Context context, Cursor cursor ) {
            int ndx = cursor.getColumnIndex(ColumnNames.Heroes.hero_image_id);
            int imgId = cursor.getInt(ndx);
            String name1 = cursor.getString(cursor.getColumnIndex(ColumnNames.Alias._id));
            String name2 = cursor.getString(cursor.getColumnIndex(ColumnNames.Heroes.name2));
            ImageView img = (ImageView) view.findViewById(R.id.img);
            img.setImageResource(imgId);
            TextView t = (TextView) view.findViewById(R.id.name1);
            t.setText(name1);
            t = (TextView) view.findViewById(R.id.name2);
            t.setText(name2);
        }

        @Override
        public View newView ( Context context, Cursor cursor, ViewGroup parent ) {
            final View view = mInflater.inflate(R.layout.list_heroes_entry,
                                                parent,false);
            return view;
        }
    }
}
